package com.anolesoft.yr.mbus.part.mapper;

import com.anolesoft.yr.mbus.part.entity.PartModelDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/11 14:51
 */
@Mapper
public interface PartModelMapper {
    @Insert("insert into part_model (" +
            "objuid," +
            "fac_code," +
            "parent_part_model_uid," +
            "part_type_uid," +
            "part_code," +
            "part_brand," +
            "part_model," +
            "vice_model," +
            "part_name," +
            "part_specification," +
            "unit," +
            "quantity," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{parentPartModelUid,jdbcType=VARCHAR}," +
            "#{partTypeUid,jdbcType=VARCHAR}," +
            "#{partCode,jdbcType=VARCHAR}," +
            "#{partBrand,jdbcType=VARCHAR}," +
            "#{partModel,jdbcType=VARCHAR}," +
            "#{viceModel,jdbcType=VARCHAR}," +
            "#{partName,jdbcType=VARCHAR}," +
            "#{partSpecification,jdbcType=VARCHAR}," +
            "#{unit,jdbcType=VARCHAR}," +
            "#{quantity,jdbcType=INTEGER}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(PartModelDO partModelDO);

    @Update("update part_model set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "parent_part_model_uid = #{parentPartModelUid,jdbcType=VARCHAR}," +
            "part_type_uid = #{partTypeUid,jdbcType=VARCHAR}," +
            "part_code = #{partCode,jdbcType=VARCHAR}," +
            "part_brand = #{partBrand,jdbcType=VARCHAR}," +
            "part_model = #{partModel,jdbcType=VARCHAR}," +
            "vice_model = #{viceModel,jdbcType=VARCHAR}," +
            "part_name = #{partName,jdbcType=VARCHAR}," +
            "part_specification = #{partSpecification,jdbcType=VARCHAR}," +
            "unit = #{unit,jdbcType=VARCHAR}," +
            "quantity = #{quantity,jdbcType=INTEGER}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR} " +
            "where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(PartModelDO partModelDO);

    @Update("update part_model set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "PartModelResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_part_model_uid", property = "parentPartModelUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_type_uid", property = "partTypeUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_code", property = "partCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_brand", property = "partBrand", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_model", property = "partModel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "vice_model", property = "viceModel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_name", property = "partName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_specification", property = "partSpecification", jdbcType = JdbcType.VARCHAR),
            @Result(column = "unit", property = "unit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "quantity", property = "quantity", jdbcType = JdbcType.INTEGER),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_type_uid", property = "partType", one = @One(select = "com.anolesoft.yr.mbus.part.mapper.PartTypeMapper.selectOneById", fetchType = FetchType.EAGER)),
            @Result(column = "parent_part_model_uid", property = "parentPartModel", one = @One(select = "com.anolesoft.yr.mbus.part.mapper.PartModelMapper.selectOneById", fetchType = FetchType.EAGER)),
    })
    @Select("select " +
            "objuid, fac_code, parent_part_model_uid, part_type_uid, part_code, part_brand," +
            "part_model, vice_model, part_name, part_specification, unit, quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_model where del_flag = 0")
    List<PartModelDO> selectAll();

    @ResultMap("PartModelResultMap")
    @Select("select " +
            "objuid, fac_code, parent_part_model_uid, part_type_uid, part_code, part_brand," +
            "part_model, vice_model, part_name, part_specification, unit, quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_model where del_flag = 0")
    List<PartModelDO> selectPageAll(Page<PartModelDO> page);

    @ResultMap("PartModelResultMap")
    @Select("select " +
            "objuid, fac_code, parent_part_model_uid, part_type_uid, part_code, part_brand," +
            "part_model, vice_model, part_name, part_specification, unit, quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_model where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    PartModelDO selectOneById(@Param("id") String id);

    @ResultMap("PartModelResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, parent_part_model_uid, part_type_uid, part_code, part_brand," +
            "part_model, vice_model, part_name, part_specification, unit, quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_model where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partTypeUid !=null and o.partTypeUid != '' \">" +
            "   and part_type_uid = #{o.partTypeUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partCode !=null and o.partCode != '' \">" +
            "   and part_code like #{o.partCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partBrand !=null and o.partBrand != '' \">" +
            "   and part_brand like #{o.partBrand, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModel !=null and o.partModel != '' \">" +
            "   and part_model like #{o.partModel, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partName !=null and o.partName != '' \">" +
            "   and part_name like #{o.partName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.unit !=null and o.unit != '' \">" +
            "   and unit like #{o.unit, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<PartModelDO> selectList(@Param("o") PartModelDO PartModelDO);

    @ResultMap("PartModelResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, parent_part_model_uid, part_type_uid, part_code, part_brand," +
            "part_model, vice_model, part_name, part_specification, unit, quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_model where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partTypeUid !=null and o.partTypeUid != '' \">" +
            "   and part_type_uid = #{o.partTypeUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partCode !=null and o.partCode != '' \">" +
            "   and part_code like #{o.partCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partBrand !=null and o.partBrand != '' \">" +
            "   and part_brand like #{o.partBrand, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModel !=null and o.partModel != '' \">" +
            "   and part_model like #{o.partModel, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partName !=null and o.partName != '' \">" +
            "   and part_name like #{o.partName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partSpecification !=null and o.partName != '' \">" +
            "   and part_specification like #{o.partSpecification, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.unit !=null and o.unit != '' \">" +
            "   and unit like #{o.unit, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<PartModelDO> selectPageList(@Param("o") PartModelDO PartModelDO, Page<PartModelDO> page);

    List<PartModelDO> selectComboxList(@Param("o") PartModelDO PartModelDO, Page<PartModelDO> page);

    @Select("select count(objuid) from part_model where del_flag = 0 and part_code = #{partCode, jdbcType=VARCHAR}")
    Integer checkUniqueForPartCode(@Param("partCode") String partCode);

    @Select("select count(objuid) from part_model where del_flag = 0 and part_code = #{partCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForPartCodeUpdate(@Param("partCode") String partCode, @Param("id") String id);

    /**
     * in objuid方法
     * @param objuid
     * @return
     */
    List<PartModelDO> selectInId(@Param("objuid") List<String> objuid);

}
